package com.ztom.daily.n2022.m3;

import java.util.ArrayList;
import java.util.List;

/**
 * 棒球比赛
 * <p>
 * https://leetcode-cn.com/problems/baseball-game/
 *
 * @author ZhangTao
 */
public class Day26CalPoints {

    public int calPoints(String[] ops) {
        if (ops == null || ops.length == 0) {
            return 0;
        }

        int res = 0;
        List<Integer> help = new ArrayList<>();
        for (String op : ops) {
            int n = help.size();
            int cur = 0;
            switch (op.charAt(0)) {
                case '+' :
                    cur = help.get(n - 1) + help.get(n - 2);
                    res += cur;
                    help.add(cur);
                    break;
                case 'D':
                    cur = help.get(n - 1) * 2;
                    res += cur;
                    help.add(cur);
                    break;
                case 'C':
                    res -= help.get(n - 1);
                    help.remove(n - 1);
                    break;
                default:
                    cur = Integer.parseInt(op);
                    res += cur;
                    help.add(cur);
                    break;
            }
        }
        return res;
    }
}
